package com.redsoil.house.ui.property.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.redsoil.house.R;
import com.redsoil.house.base.BaseFragmentActivity;
import com.redsoil.house.ui.property.contract.PropertyContract;
import com.redsoil.house.ui.property.fragment.PropertyFileFragment;
import com.redsoil.house.ui.property.fragment.PropertyIncomeFragment;
import com.redsoil.house.ui.property.fragment.PropertyInfoFragment;
import com.redsoil.house.ui.property.fragment.PropertyLeaseFragment;
import com.redsoil.house.ui.property.presenter.PropertyPresenter;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * 物业详情
 */
public class PropertyActivity extends BaseFragmentActivity<PropertyPresenter> implements PropertyContract.View, XBaseTopBarImp{

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    @BindView(R.id.info_tv)
    TextView info_tv;

    @BindView(R.id.lease_tv)
    TextView lease_tv;

    @BindView(R.id.income_tv)
    TextView income_tv;

    @BindView(R.id.file_tv)
    TextView file_tv;

    //当前要显示的
    private int position = FIRST;
    //以前显示的
    private int prePosition = FIRST;

    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FOUR = 3;

    private SupportFragment[] mFragments = new SupportFragment[4];

    private String propertyId;

    @Override
    protected int getLayout() {
        return R.layout.activity_property;
    }

    @Override
    protected void initEventAndData() {

        propertyId = getIntent().getStringExtra("id");

        main_topbar.setxBaseTopBarImp(this);

        SupportFragment firstFragment = findFragment(PropertyInfoFragment.class);
        if (firstFragment == null) {
            mFragments[FIRST] = PropertyInfoFragment.newInstance(propertyId);
            mFragments[SECOND] = PropertyLeaseFragment.newInstance(propertyId);
            mFragments[THIRD] = PropertyIncomeFragment.newInstance(propertyId);
            mFragments[FOUR] = PropertyFileFragment.newInstance(propertyId);

            loadMultipleRootFragment(R.id.property_container, FIRST,
                    mFragments[FIRST], mFragments[SECOND], mFragments[THIRD],mFragments[FOUR]);
        }else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题

            // 这里我们需要拿到mFragments的引用,也可以通过getChildFragmentManager.findFragmentByTag自行进行判断查找(效率更高些),用下面的方法查找更方便些
            mFragments[FIRST] = firstFragment;
            mFragments[SECOND] = findFragment(PropertyLeaseFragment.class);
            mFragments[THIRD] = findFragment(PropertyIncomeFragment.class);
            mFragments[FOUR] = findFragment(PropertyFileFragment.class);
        }
        onInfoTvClick();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @OnClick(R.id.info_tv)
    void onInfoTvClick()
    {
        changeShow(FIRST);
        info_tv.setSelected(true);
        info_tv.setTextColor(ContextCompat.getColor(mContext, R.color.whitefff));
        main_topbar.setMiddleText(getResources().getString(R.string.property_info));
    }

    @OnClick(R.id.lease_tv)
    void onLeaseTvClick()
    {
        changeShow(SECOND);
        lease_tv.setSelected(true);
        lease_tv.setTextColor(ContextCompat.getColor(mContext, R.color.whitefff));
        main_topbar.setMiddleText(getResources().getString(R.string.property_lease));
    }

    @OnClick(R.id.income_tv)
    void onIncomeTvClick()
    {
        changeShow(THIRD);
        income_tv.setSelected(true);
        income_tv.setTextColor(ContextCompat.getColor(mContext, R.color.whitefff));
        main_topbar.setMiddleText(getResources().getString(R.string.property_income));
    }

    @OnClick(R.id.file_tv)
    void onFileTvClick()
    {
        changeShow(FOUR);
        file_tv.setSelected(true);
        file_tv.setTextColor(ContextCompat.getColor(mContext, R.color.whitefff));
        main_topbar.setMiddleText(getResources().getString(R.string.property_file));
    }

    private void changeShow(int _position){
        info_tv.setSelected(false);
        lease_tv.setSelected(false);
        income_tv.setSelected(false);
        file_tv.setSelected(false);
        info_tv.setTextColor(ContextCompat.getColor(mContext, R.color.black029));
        lease_tv.setTextColor(ContextCompat.getColor(mContext, R.color.black029));
        income_tv.setTextColor(ContextCompat.getColor(mContext, R.color.black029));
        file_tv.setTextColor(ContextCompat.getColor(mContext, R.color.black029));
        if(position == _position) return;
        prePosition = position;
        position = _position;
        showHideFragment(mFragments[position], mFragments[prePosition]);
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context,String id) {
        Intent intent = new Intent(context, PropertyActivity.class);
        intent.putExtra("id",id);
        return intent;
    }

    @Override
    public void left() {
        finish();
    }

    @Override
    public void right() {}
}
